package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class UpPayPsdActivity extends BaseActivity implements View.OnClickListener{

    private ImageView upPayPsd_back,upPayPsd_newPsd_img,upPayPsd_configPsd_img;
    private EditText upPayPsd_oldPsd,upPayPsd_newPsd,upPayPsd_configPsd;
    private TextView upPayPsd_sure;

    private boolean flag1 = true;
    private boolean flag2 = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_paypasword);
        initView();
    }

    private void initView(){
        upPayPsd_back = (ImageView) findViewById(R.id.upPayPsd_back);
        upPayPsd_back.setOnClickListener(this);
        upPayPsd_newPsd_img = (ImageView) findViewById(R.id.upPayPsd_newPsd_img);
        upPayPsd_newPsd_img.setOnClickListener(this);
        upPayPsd_configPsd_img = (ImageView) findViewById(R.id.upPayPsd_configPsd_img);
        upPayPsd_configPsd_img.setOnClickListener(this);
        upPayPsd_oldPsd = (EditText) findViewById(R.id.upPayPsd_oldPsd);
        upPayPsd_newPsd = (EditText) findViewById(R.id.upPayPsd_newPsd);
        upPayPsd_configPsd = (EditText) findViewById(R.id.upPayPsd_configPsd);
        upPayPsd_sure = (TextView) findViewById(R.id.upPayPsd_sure);
        upPayPsd_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.upPayPsd_back:
                finish();
                break;
            case R.id.upPayPsd_newPsd_img:
                if (flag1)
                {
                    upPayPsd_newPsd_img.setImageResource(R.mipmap.password_open);
                    upPayPsd_newPsd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag1 = false;
                }else {
                    upPayPsd_newPsd_img.setImageResource(R.mipmap.password_close);
                    upPayPsd_newPsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag1 = true;
                }
                break;
            case R.id.upPayPsd_configPsd_img:
                if (flag2)
                {
                    upPayPsd_configPsd_img.setImageResource(R.mipmap.password_open);
                    upPayPsd_configPsd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag2 = false;
                }else {
                    upPayPsd_configPsd_img.setImageResource(R.mipmap.password_close);
                    upPayPsd_configPsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag2 = true;
                }
                break;
            case R.id.upPayPsd_sure:
                upPayPsd();
                break;
        }
    }

    private void upPayPsd()
    {
        String oldPsd = upPayPsd_oldPsd.getText().toString();
        if (oldPsd.isEmpty()){
            showToast("请输入原密码");
            return;
        }
        String newPsd = upPayPsd_newPsd.getText().toString();
        if (newPsd.isEmpty()){
            showToast("请输入新密码");
            return;
        }
        if (newPsd.length()<6){
            showToast("密码不得少于6位");
            return;
        }
        String configPsd = upPayPsd_configPsd.getText().toString();
        if (configPsd.isEmpty()){
            showToast("请再次输入密码");
            return;
        }
        if (!newPsd.equals(configPsd)){
            showToast("密码输入不一致");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("passwd",newPsd);
        map.put("passwd_old",oldPsd);
        map.put("method","resert");
        XUtil.Post(UrlConfig.UP_PAYPSD_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("修改支付密码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    UpPayPsdActivity.this.finish();
                }
                showToast(jsonObject.getString("msg"));
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("修改支付密码失败");
            }
        });

    }
}
